/*
 * @Date: 2024-04-02 16:05:22
 * @LastEditors: bingkui.wu
 * @LastEditTime: 2024-04-02 16:06:16
 * @Description: 获取教材和年级信息
 */
import { REQUEST } from "#type/request";
import Response from "@common/Response";
import { BookUnit } from "@models/BookUnit";
import { BookGrade } from "@models/BookGrade";
import { BookWord } from "@models/BookWord";
import { BookUnitWord } from "@models/BookUnitWord";

const root: REQUEST.BASIC = {
  auth: false,
  method: "get",
  request: async (ctx: any) => {
    const { unitId } = ctx.request.query;

    const unit = await BookUnit.findOne({ unitId })
      .lean()
      .select("-_id -__v")
      .exec();
    const wordsId = await BookUnitWord.find({ unitId })
      .lean()
      .select("-_id -__v")
      .exec();

    const words = await BookWord.find({
      wordId: { $in: wordsId.map((m) => m.wordId) },
    })
      .lean()
      .select("-_id -__v")
      .exec();

    // return Response.Success(wordsId);
    return Response.Success({ ...unit, ...{ words } });
  },
};

export default root;
